# -*- coding: utf-8 -*-
"""
Created on Fri Sep 22 16:49:07 2017

@author: xuanlei
"""

import math
import random
import DAencoder
#from sklearn.utils import shuffle

#===============================================================================
# 数据滑窗：平衡正负样本数量，可对gap进行调整
#===============================================================================
def get_moving_data(window_dataframe,run):
    if run == 1:
        gap = 10 #20  70 95  250
    else:
        gap = 10
    rows = window_dataframe.shape[0]
    window_num = math.floor(rows/gap)
    index = 0
    result_list = []
    for i in range(window_num):
        tran_window_data = window_dataframe.iloc[index:index+10,:]
        if tran_window_data.shape[0] == 10 and (tran_window_data['label'].sum()==10 or tran_window_data['label'].sum()== 0):#约束滑窗内标签一致，因为编码后的数据把0和1连起来了
            result_list.append(tran_window_data)
        index += gap
        if index > rows:
            break
    return result_list

#===============================================================================
# 遍历[df1,df2...]
#===============================================================================
def get_all_data(window_data,a=1):
    l_data = []
    for window in window_data:
        l_data.extend(get_moving_data(window,a))
    return l_data


def start(run,err):
    all_data = []
    all_data1 = get_all_data(run)
    #all_data2 = all_data1.extend(get_all_data(err,0))
    all_data2 = get_all_data(err,0)
#    all_data.extend(all_data1)
    all_data.extend(all_data1)
    all_data.extend(all_data2)
    print(">>>>>>>>>>>>>>>>>>>>>>>>>>")
    print('滑窗数据已提取完毕！')
    print("<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
    return all_data1,all_data2,all_data

def merged(wdata):
    data = pd.DataFrame()
    for item in wdata:
        data = data.append(item)
    return data

def getxy(df):
    f_list = ['wind_speed_max', 'generator_speed_max', 'tip_pressure_max',
       'system_pressure_max', 'wind_direction_max', 'yaw_angle_max',
       'gear_oil_temp_max', 'gear_box_bearing_temp_max',
       'environment_temperature_max', 'engine_room_temp_max',
       'generator_front_shaft_temp_max', 'generator_back_shaft_temp_max',
       'generator_temp_max', 'A_phase_current_max', 'B_phase_current_max',
       'C_phase_current_max', 'AB_phase_voltage_max', 'BC_phase_voltage_max',
       'CA_phase_voltage_max', 'real_power_max', 'wind_speed_mean',
       'generator_speed_mean', 'tip_pressure_mean', 'system_pressure_mean',
       'wind_direction_mean', 'yaw_angle_mean', 'gear_oil_temp_mean',
       'gear_box_bearing_temp_mean', 'environment_temperature_mean',
       'engine_room_temp_mean', 'generator_front_shaft_temp_mean',
       'generator_back_shaft_temp_mean', 'generator_temp_mean',
       'A_phase_current_mean', 'B_phase_current_mean', 'C_phase_current_mean',
       'AB_phase_voltage_mean', 'BC_phase_voltage_mean',
       'CA_phase_voltage_mean', 'real_power_mean', 'wind_speed_min',
       'generator_speed_min', 'tip_pressure_min', 'system_pressure_min',
       'wind_direction_min', 'yaw_angle_min', 'gear_oil_temp_min',
       'gear_box_bearing_temp_min', 'environment_temperature_min',
       'engine_room_temp_min', 'generator_front_shaft_temp_min',
       'generator_back_shaft_temp_min', 'generator_temp_min',
       'A_phase_current_min', 'B_phase_current_min', 'C_phase_current_min',
       'AB_phase_voltage_min', 'BC_phase_voltage_min', 'CA_phase_voltage_min',
       'real_power_min']
    xr = df[f_list]
    yr = df['label']
    return xr,yr
    
#===============================================================================
# 划分训练集与测试集
#===============================================================================
    
def split_data(): 
# =============================================================================
#     result_err_s = [result_err_e[i] for i in [0,2,3,6,8,9,11,12,14,15,16,19,20,23]]
#     result_err_s_test = [result_err_e[i] for i in [10,22,17]]
#     result_run_s = [result_run[i] for i in [0,1,2,4,6,7,9,18,20]]
#     result_run_s.append(result_err_r[15])
#     result_run_s.append(result_err_r[20])
#     result_run_s.append(result_err_r[5])
#     result_run_s_test = [result_run[i] for i in [12,10,19]]
#     result_run_s_test.append(result_err_r[18])
#     result_run_s_test.append(result_err_r[10])
#     result_run_s_test.extend(result_err_s_test)
#     result_test = result_run_s_test
#     return result_err_s,result_run_s,result_test
# =============================================================================
    train_err = [result_err_e[i] for i in [0,5,6,7,8,9,10,11,12,13,14,15,16,17,21,22,23]]
    train_run = [result_err_r[i] for i in [0,1,2,3,4,5,6,7,8,9,14,15,16,17,18,19,20,21,22,23]]
    train_nomal_run = [result_run[i] for i in [0,6,7,12,9,11,13,14,15,16,19,20]]
#    
    test_err = [result_err_e[i] for i in [3,18,20,2,19]]
    test_run = [result_err_r[i] for i in [11,12,10,13]]
    test_nomal_run = [result_run[i] for i in [8,10,17]]

    
    train_err.extend(train_run)
    train_err.extend(train_nomal_run)
    train_data = train_err
    train_data = merged(train_data)
    
    
    
    xr,yr = getxy(train_data)
    
    test_err.extend(test_run)
    test_err.extend(test_nomal_run)
    test_data = test_err
    test_data = merged(test_data)
    
    
    test_xr,test_yr = getxy(test_data)
    
    ### 数据编码
    result_train2,result_test2 = DAencoder.start_train()
    #############   生成时间序列滑窗
    
    trainx = pd.DataFrame(result_train2)
    trainx['label'] = list(yr)
    
    testx = pd.DataFrame(result_test2)
    testx['label'] = list(test_yr)
    
    ls_train2 = get_moving_data(trainx,1)
    ls_re_test2 = get_moving_data(testx,1)
    
    
    random.shuffle(ls_train2)
    random.shuffle(ls_re_test2)
    
    return ls_train2,ls_re_test2
    


   